package com.wing.knife.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.annotation.RequestObjectFieldIgnore;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.platform.feign.PlatformClient;
import com.wing.product.model.entity.ProductType;
import com.wing.product.model.request.ProductTypeForm;
import com.wing.product.model.response.ProductTypeVO;
import com.wing.product.service.ProductTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "Admin-产品类型管理", tags = "#/knife/productTypeKnife")
@RestController("productTypeKnifeController")
@RequestMapping("knife/admin/productTypeKnife")
public class ProductTypeKnifeController {

    @Autowired
    private ProductTypeService productTypeService;
    @Autowired
    private PlatformClient platformClient;



    @ApiOperation(value = "查询所有产品类型", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "name", value = "名称", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "code", value = "编号", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<ProductTypeVO> list(
            @RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer limit
            , @RequestParam(defaultValue = "create_time") String sort
            , @RequestParam(defaultValue = "asc") String order
            , String name
            , String code
            ,@RequestParam(defaultValue = "enterprise")  ProductType.DataSourceType dataSourceType
    ){

        Page<ProductTypeVO> pg = new Page<>(page, limit);
        Map paramMap=new HashMap();
        paramMap.put("sort",StrUtil.toUnderlineCase(sort));
        paramMap.put("order",StrUtil.toUnderlineCase(order));
        paramMap.put("name",name);
        paramMap.put("code",code);
        /*paramMap.put("categoryType",4);*/
        if (dataSourceType!=null){
            paramMap.put("dataSourceType",dataSourceType.getValue());
        }
        IPage<ProductTypeVO> list = productTypeService.pageList4app(pg,paramMap);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }



    @ApiOperation(value = "添加产品类型", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@Validated(ProductTypeForm.Add.class) ProductTypeForm productTypeForm) {

        if (productTypeService.add(productTypeForm) > 0) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }

    @ApiOperation(value = "启用产品类型", notes = "启用")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/enable")
    public JsonResult enable(@Validated(ProductTypeForm.Add.class) ProductTypeForm productTypeForm) {

        ProductType productType=new ProductType();
        productType.setId(productTypeForm.getId());
        productType.setIsDeleted(true);
        if (productTypeService.updateById(productType)) {
            return JsonResult.success("启用成功");
        }
        return JsonResult.fail("启用失败");
    }

    @ApiOperation(value = "停用产品类型", notes = "停用")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/disable")
    public JsonResult disable(@Validated(ProductTypeForm.Add.class) ProductTypeForm productTypeForm) {

        ProductType productType=new ProductType();
        productType.setId(productTypeForm.getId());
        productType.setIsDeleted(false);
        if (productTypeService.updateById(productType)) {
            return JsonResult.success("停用成功");
        }
        return JsonResult.fail("停用失败");
    }




    @ApiOperation(value = "修改产品类型", notes = "修改")
    @PutMapping("/update")
    public JsonResult update(@Validated(ProductTypeForm.Update.class) ProductTypeForm productTypeForm) {

        ProductType productType = productTypeService.getById(productTypeForm.getId());
        if(productType == null){
        return JsonResult.fail("修改失败！");
        }
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(ProductType.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(productTypeForm, productType, copyOptions);
        if (productTypeService.updateById(productType)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }



    @ApiOperation(value = "删除产品类型", notes = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productTypeId", value = "产品类型id", required = true, dataType = "Integer", paramType = "path")
    })
    @DeleteMapping("/{id}" )
    public JsonResult delete(@PathVariable("id") Long productTypeId) {

        if (productTypeService.removeById(productTypeId)) {
            return JsonResult.success("删除成功");
        }
        return JsonResult.fail("删除失败");
    }
    @GetMapping("/getProductTypeVoById")
    public JsonResult<ProductTypeVO> getProductTypeVoById(Long productTypeId){

        ProductTypeVO productTypeVO = productTypeService.get4appById(productTypeId);
        return JsonResult.success(productTypeVO);
    }
    @GetMapping("/getProductTypeVOAll")
    public JsonResult getProductTypeVOAll(){

        QueryWrapper<ProductType> wrapper = new QueryWrapper<>();
        wrapper.eq("data_source_type", ProductType.DataSourceType.enterprise.getValue());
        List<ProductType> productType = productTypeService.list(wrapper);
        return JsonResult.success(productType);
    }

    @GetMapping("/getProductTypeByType")
    public JsonResult getProductTypeByType(@RequestParam Integer type){

        QueryWrapper<ProductType> wrapper = new QueryWrapper<>();
        wrapper.eq("type", type);
        wrapper.eq("data_source_type", ProductType.DataSourceType.enterprise.getValue());
        List<ProductType> productType = productTypeService.list(wrapper);
        return JsonResult.success(productType);
    }

    /**
     * 查询所有产品类型
     */
    @GetMapping("/getAllKnife")
    public JsonResult getAll(){

        QueryWrapper<ProductType> wrapper = new QueryWrapper<>();
        wrapper.eq("type", "5");
        wrapper.eq("data_source_type", ProductType.DataSourceType.enterprise.getValue());
        List<ProductType> list = productTypeService.list(wrapper);
        return JsonResult.success(list);
    }


    /**
     * 根据id查询产品类型 平台
     */
    @GetMapping("/getPlatformByType")
    public JsonResult getPlatformByType(@RequestParam Integer type){
        JsonResult productTypeByType = platformClient.getProductTypeByType(type);
        return JsonResult.success(productTypeByType);
    }
}
